unit uCompDM;

interface

uses
  System.SysUtils, System.Classes, Data.DB, Data.Win.ADODB, System.IniFiles,
  System.Generics.Collections, Vcl.ImgList, Vcl.Controls, MemTableDataEh,
  MemTableEh, DataDriverEh;

type
  EConnectException = class(Exception);

  EInsExcelTempException = class(Exception);

  EInsSvodDuplesException = class(Exception);

  EInsSvodInvException = class(Exception);

  EInsSprProvException = class(Exception);

  EInsTmpComeralInvException = class(Exception);

  EInsTmpComeralProvException = class(Exception);

type
  TCompDM = class(TDataModule)
    SvodMDBConnection: TADOConnection;
    SelectPlatPredpr: TADOQuery;
    SelectPlatPredprDS: TDataSource;
    ImageList_16: TImageList;
    ImageList_24: TImageList;
    CompareMemTableEh: TMemTableEh;
    CompareMemTableEhDS: TDataSource;
    CrExcelTemp: TADOQuery;
    InsExcelTempQ: TADOQuery;
    SelectSvodInvDSDEh: TDataSetDriverEh;
    ObjMDBConnection: TADOConnection;
    InsInvProvData: TADOQuery;
    SelectInvProvData: TADOQuery;
    SelectDuplicateProv: TADOQuery;
    CrSvodDuples: TADOQuery;
    CrSvodInv: TADOQuery;
    CrInvProv: TADOQuery;
    InsSvodDuplesQ: TADOQuery;
    SelectInvProvDatafullName: TStringField;
    SelectInvProvDatadocumentType: TWordField;
    SelectInvProvDatainvStatus: TSmallintField;
    SelectInvProvDatadocNumber: TStringField;
    SelectInvProvDatadocDate: TDateTimeField;
    SelectInvProvDatasprSum: TBCDField;
    SelectInvProvDatatotalVat: TBCDField;
    SelectInvProvDatatotalCost: TBCDField;
    SelectInvProvDatadopUnn: TSmallintField;
    SelectInvProvDataNumBuhSpr: TStringField;
    SelectInvProvDataidPredpr: TIntegerField;
    SelectInvProvDataglobalcode: TLargeintField;
    SelectDuplicateProvNumBuhSpr: TStringField;
    SelectDuplicateProvdocNumber: TStringField;
    SelectDuplicateProvdocDate: TDateTimeField;
    SelectDuplicateProvsprSum: TBCDField;
    SelectDuplicateProvnumBuhinv: TStringField;
    SelectDuplicateProvfullName: TStringField;
    SelectDuplicateProvtotalVat: TBCDField;
    SelectDuplicateProvnamePredpr: TStringField;
    SelectDuplicateProvdopUNN: TSmallintField;
    SelectInvProvDatadateTrans: TDateTimeField;
    SelectInvProvDatatotalCostVat: TBCDField;
    InsSvodInvQ: TADOQuery;
    SelectInvProvDataSvplace: TStringField;
    SelectSvodDuplesFilial: TADOQuery;
    SelectSvodDuplesDS: TDataSource;
    SelectSvodInv: TADOQuery;
    SelectSvodDuplesFilialnumbuhdup: TStringField;
    SelectSvodDuplesFilialdocnumberdup: TStringField;
    SelectSvodDuplesFilialdocdatedup: TDateTimeField;
    SelectSvodDuplesFilialsumdup: TBCDField;
    SelectSvodDuplesFilialnumbuhsv: TStringField;
    SelectSvodDuplesFilialinvfullname: TStringField;
    SelectSvodDuplesFilialinvtotalvat: TBCDField;
    SelectSvodDuplesFilialnamepredpr: TStringField;
    SelectSvodDuplesFilialdopUNN: TSmallintField;
    SelectSvodInvidPredpr: TLargeintField;
    SelectSvodInvinvoicefullname: TStringField;
    SelectSvodInvdatetrans: TDateTimeField;
    SelectSvodInvstatus: TWordField;
    SelectSvodInvnamepredpr: TStringField;
    SelectSvodInvdocnumber: TStringField;
    SelectSvodInvdocdate: TDateTimeField;
    SelectSvodInvndsexcel: TBCDField;
    SelectSvodInvndsspr: TBCDField;
    SelectSvodInvndsinvoice: TBCDField;
    SelectSvodInvtotalcostinv: TBCDField;
    SelectSvodInvtotalcostvatinv: TBCDField;
    SelectSvodInvfilial: TSmallintField;
    SelectSvodInvsvplace: TStringField;
    CompareMemTableEhidPredpr: TLargeintField;
    CompareMemTableEhinvoicefullname: TStringField;
    CompareMemTableEhdatetrans: TDateTimeField;
    CompareMemTableEhstatus: TWordField;
    CompareMemTableEhnamepredpr: TStringField;
    CompareMemTableEhdocnumber: TStringField;
    CompareMemTableEhdocdate: TDateTimeField;
    CompareMemTableEhndsexcel: TBCDField;
    CompareMemTableEhndsspr: TBCDField;
    CompareMemTableEhndsinvoice: TBCDField;
    CompareMemTableEhtotalcostinv: TBCDField;
    CompareMemTableEhtotalcostvatinv: TBCDField;
    CompareMemTableEhfilial: TSmallintField;
    CompareMemTableEhsvplace: TStringField;
    CompareMemTableEhStatusText: TStringField;
    SelectSprProvQ: TADOQuery;
    SelectSprProvDS: TDataSource;
    InsSprProvQ: TADOQuery;
    CrSprProv: TADOQuery;
    SelectSprProvObj: TADOQuery;
    SelectSprProvObjSumm: TBCDField;
    SelectSprProvObjDataDoc: TDateTimeField;
    SelectSprProvObjNumDoc: TStringField;
    SelectSprProvObjnumDocPart: TStringField;
    SelectSprProvObjNumBuhSpr: TStringField;
    SelectSprProvObjglobalcode: TStringField;
    SelectSprProvObjdopUNN: TSmallintField;
    SelectInvProvDataNumDocPart: TStringField;
    SelectSvodInvnumbuhspr: TStringField;
    SelectSvodInvnumdocpart: TStringField;
    CompareMemTableEhnumbuhspr: TStringField;
    CompareMemTableEhnumdocpart: TStringField;
    SelectSprProvObjNSF: TStringField;
    SelectSvodDuplesOblpo: TADOQuery;
    SelectSvodDuplesOblpoDS: TDataSource;
    SelectSvodDuplesOblpoidPredpr: TLargeintField;
    SelectSvodDuplesOblpoinvoicefullname: TStringField;
    SelectSvodDuplesOblpodatetrans: TDateTimeField;
    SelectSvodDuplesOblpostatus: TWordField;
    SelectSvodDuplesOblponamePredpr: TStringField;
    SelectSvodDuplesOblpodocnumber: TStringField;
    SelectSvodDuplesOblpodocdate: TDateTimeField;
    SelectSvodDuplesOblpondsspr: TBCDField;
    SelectSvodDuplesOblpondsinvoice: TBCDField;
    SelectSvodDuplesOblpototalcostinv: TBCDField;
    SelectSvodDuplesOblpototalcostvatinv: TBCDField;
    SelectSvodDuplesOblpofilial: TSmallintField;
    SelectSvodDuplesOblponumbuhspr: TStringField;
    SelectSvodDuplesOblponumdocpart: TStringField;
    SelectOblDopUnn: TADOQuery;
    CompareMemTableEhndsdiff: TBCDField;
    SelectSvodInvrecipientname: TStringField;
    SelectSvodInvsignerfio: TStringField;
    CompareMemTableEhrecipientname: TStringField;
    CompareMemTableEhsignerfio: TStringField;
    TmpComeralInvMemTableEh: TMemTableEh;
    TmpComeralInvMemTableEhDS: TDataSource;
    SelectTmpComeralInvDSDEh: TDataSetDriverEh;
    SelectTmpComeralInv: TADOQuery;
    SelectTmpComeralInvidRecord: TAutoIncField;
    SelectTmpComeralInvidPredpr: TLargeintField;
    SelectTmpComeralInvinvoicefullname: TStringField;
    SelectTmpComeralInvdatetrans: TDateTimeField;
    SelectTmpComeralInvstatus: TWordField;
    SelectTmpComeralInvrecipientname: TStringField;
    SelectTmpComeralInvsignerfio: TStringField;
    SelectTmpComeralInvnamepredpr: TStringField;
    SelectTmpComeralInvdocnumber: TStringField;
    SelectTmpComeralInvdocdate: TDateTimeField;
    SelectTmpComeralInvndsexcel: TBCDField;
    SelectTmpComeralInvndsspr: TBCDField;
    SelectTmpComeralInvndsinvoice: TBCDField;
    SelectTmpComeralInvtotalcostinv: TBCDField;
    SelectTmpComeralInvtotalcostvatinv: TBCDField;
    SelectTmpComeralInvfilial: TSmallintField;
    SelectTmpComeralInvsvplace: TStringField;
    SelectTmpComeralInvnumbuhspr: TStringField;
    SelectTmpComeralInvnumdocpart: TStringField;
    SelectTmpComeralInvhide: TBooleanField;
    TmpComeralInvMemTableEhidRecord: TAutoIncField;
    TmpComeralInvMemTableEhhide: TBooleanField;
    SelectTmpComeralProvDS: TDataSource;
    SelectTmpComeralProv: TADOQuery;
    TmpComeralInvMemTableEhidPredpr: TLargeintField;
    TmpComeralInvMemTableEhinvoicefullname: TStringField;
    TmpComeralInvMemTableEhdatetrans: TDateTimeField;
    TmpComeralInvMemTableEhstatus: TWordField;
    TmpComeralInvMemTableEhrecipientname: TStringField;
    TmpComeralInvMemTableEhsignerfio: TStringField;
    TmpComeralInvMemTableEhnamepredpr: TStringField;
    TmpComeralInvMemTableEhdocnumber: TStringField;
    TmpComeralInvMemTableEhdocdate: TDateTimeField;
    TmpComeralInvMemTableEhndsexcel: TBCDField;
    TmpComeralInvMemTableEhndsspr: TBCDField;
    TmpComeralInvMemTableEhndsinvoice: TBCDField;
    TmpComeralInvMemTableEhtotalcostinv: TBCDField;
    TmpComeralInvMemTableEhtotalcostvatinv: TBCDField;
    TmpComeralInvMemTableEhfilial: TSmallintField;
    TmpComeralInvMemTableEhsvplace: TStringField;
    TmpComeralInvMemTableEhnumbuhspr: TStringField;
    TmpComeralInvMemTableEhnumdocpart: TStringField;
    TmpComeralInvMemTableEhStatusText: TStringField;
    InsTmpComeralInvQ: TADOQuery;
    InsTmpComeralProvQ: TADOQuery;
    RewriteInsTmpComeralProv: TADOQuery;
    DelTmpComeralInvQ: TADOQuery;
    TmpComeralInvMemTableEhndsdiff: TBCDField;
    procedure DataModuleCreate(Sender: TObject);
    procedure CompareMemTableEhCalcFields(DataSet: TDataSet);
    procedure TmpComeralInvMemTableEhCalcFields(DataSet: TDataSet);
  private
    { Private declarations }
    function GetStatusText(Index: SmallInt): string;
  public
    { Public declarations }
    ObjectsDict: TDictionary<string, string>;
    procedure ConnectToObject(server: string);
    function UpdateDecimalSeparator(const s: string): string;
    procedure InsertExcelTemp(idPredpr: int64; namePredpr, invoicefullname,
      docnumber, svplace, recipientname, signerfio: string; datetrans, docdate:
      TDateTime; status: SmallInt; ndsexcel, totalcostinv, totalcostvatinv: Currency; dopunn: Variant);
    procedure InsertSvodDuples(numbuhdup, docnumberdup, numbuhsv, invfullname,
      namepredpr: string; docdatedup: TDateTime; sumdup, invtotalvat: Currency; dopUNN: SmallInt);
    procedure InsertSvodInv(idPredpr, invoicefullname, datetrans, status, ndsspr,
      ndsinvoice, totalcostinv, totalcostvatinv, svplace, numbuhspr, numdocpart:
      Variant; docnumber: string; docdate: TDateTime; filial: SmallInt);
    procedure InsertSprProv(numbuhspr, numdoc, numdocpart, NSF: string; datadoc:
      TDateTime; summ: Currency; globalcode: Variant; filial: SmallInt);
    procedure SelectSprProv(Query: TADOQuery; numdoc, numdocpart, numbuhspr, globalcode,
      invfullname: string; datadoc: TDateTime; filial: SmallInt);
    procedure InsertTmpComeralInv(idPredpr, invoicefullname, datetrans, status,
      recipientname, signerfio, namepredpr, docnumber, docdate, ndsexcel, ndsspr,
      ndsinvoice, totalcostinv, totalcostvatinv, filial, svplace, numbuhspr, numdocpart: Variant);
    procedure InsertTpmComeralProv(numbuhspr, numdoc, numdocpart, NSF, datadoc,
      summ, globalcode, filial: Variant);
  end;

const
  connstr = 'Provider=SQLOLEDB.1;Password=transpass;Persist Security Info=True;' + 'User ID=transport;';
  // Переменная для подстановки коннектов:
  // True - коннект к локальным базам,
  // Flase - коннекты из файла к реальным базам
  DEBUG = False;

var
  CompDM: TCompDM;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TCompDM.ConnectToObject(server: string);
begin
  // Подключение к филиалу
  try
    with ObjMDBConnection do
    begin
      Close;
      ConnectionString := connstr + 'Initial Catalog=MainDataBase;Data Source=' + server;
      if DEBUG then
        ConnectionString := 'Provider=SQLOLEDB.1;Password=transpass;' +
          'Persist Security Info=True;User ID=transport;' + 'Initial Catalog=maindatabase;Data Source=.';
      Open;
    end;
  except
    on e: Exception do
      raise EConnectException.Create(e.Message);
  end;
end;

procedure TCompDM.SelectSprProv(Query: TADOQuery; numdoc, numdocpart, numbuhspr, globalcode,
  invfullname: string; datadoc: TDateTime; filial: SmallInt);
begin
  // Выборка проводок из временной таблицы
  with Query do
  begin
    Close;
    Parameters.ParamByName('numdoc').Value := numdoc;
    Parameters.ParamByName('numdocpart').Value := numdocpart;
    Parameters.ParamByName('datadoc').Value := datadoc;
    Parameters.ParamByName('numbuhspr').Value := numbuhspr;
    Parameters.ParamByName('globalcode').Value := globalcode;
    Parameters.ParamByName('filial').Value := filial;
    Parameters.ParamByName('invfullname').Value := invfullname;
    Open;
  end;
end;

procedure TCompDM.TmpComeralInvMemTableEhCalcFields(DataSet: TDataSet);
begin
  TmpComeralInvMemTableEhStatusText.AsString := GetStatusText(TmpComeralInvMemTableEhStatus.AsInteger);
  TmpComeralInvMemTableEhndsdiff.Value := TmpComeralInvMemTableEhndsexcel.AsCurrency
    - TmpComeralInvMemTableEhndsspr.AsCurrency;
end;

procedure TCompDM.InsertExcelTemp(idPredpr: int64; namePredpr, invoicefullname,
  docnumber, svplace, recipientname, signerfio: string; datetrans, docdate:
  TDateTime; status: SmallInt; ndsexcel, totalcostinv, totalcostvatinv: Currency; dopunn: Variant);
begin
  // Вставка записи во временную таблицу #svexceltmp
  with InsExcelTempQ do
  try
    Parameters.ParamByName('idPredpr').Value := idPredpr;
    Parameters.ParamByName('namePredpr').Value := namePredpr;
    Parameters.ParamByName('invoicefullname').Value := invoicefullname;
    Parameters.ParamByName('docnumber').Value := docnumber;
    Parameters.ParamByName('svplace').Value := svplace;
    Parameters.ParamByName('datetrans').Value := datetrans;
    Parameters.ParamByName('docdate').Value := docdate;
    Parameters.ParamByName('status').Value := status;
    Parameters.ParamByName('recipientname').Value := recipientname;
    Parameters.ParamByName('dopunn').Value := dopunn;
    Parameters.ParamByName('signerfio').Value := signerfio;
    Parameters.ParamByName('ndsexcel').Value := ndsexcel;
    Parameters.ParamByName('totalcostinv').Value := totalcostinv;
    Parameters.ParamByName('totalcostvatinv').Value := totalcostvatinv;
    ExecSQL;
  except
    on E: Exception do
      raise EInsExcelTempException.Create('Ошибка при вставке информации из ' +
        'файла камерального контроля с ' + 'текстом: ' + E.Message);
  end;
end;

procedure TCompDM.InsertSvodDuples(numbuhdup, docnumberdup, numbuhsv,
  invfullname, namepredpr: string; docdatedup: TDateTime; sumdup, invtotalvat: Currency; dopUNN: SmallInt);
begin
  // Вставка записи во временную таблицу #svod_duples
  with InsSvodDuplesQ do
  try
    Parameters.ParamByName('numbuhdup').Value := numbuhdup;
    Parameters.ParamByName('docnumberdup').Value := docnumberdup;
    Parameters.ParamByName('docdatedup').Value := docdatedup;
    Parameters.ParamByName('sumdup').Value := sumdup;
    Parameters.ParamByName('numbuhsv').Value := numbuhsv;
    Parameters.ParamByName('invfullname').Value := invfullname;
    Parameters.ParamByName('invtotalvat').Value := invtotalvat;
    Parameters.ParamByName('namepredpr').Value := namepredpr;
    Parameters.ParamByName('dopUNN').Value := dopUNN;
    ExecSQL;
  except
    on E: Exception do
      raise EInsSvodDuplesException.Create('Ошибка вставки записи во временную'
        + ' таблицу #svod_duples с текстом: ' + e.Message);
  end;
end;

procedure TCompDM.InsertSvodInv(idPredpr, invoicefullname, datetrans, status,
  ndsspr, ndsinvoice, totalcostinv, totalcostvatinv, svplace, numbuhspr,
  numdocpart: Variant; docnumber: string; docdate: TDateTime; filial: SmallInt);
begin
  // Вставка записи во временную таблицу #svod_inv
  with InsSvodInvQ do
  try
    Parameters.ParamByName('idPredpr').Value := idPredpr;
    Parameters.ParamByName('invoicefullname').Value := invoicefullname;
    Parameters.ParamByName('datetrans').Value := datetrans;
    Parameters.ParamByName('status').Value := status;
    Parameters.ParamByName('docnumber').Value := docnumber;
    Parameters.ParamByName('docdate').Value := docdate;
    Parameters.ParamByName('ndsspr').Value := ndsspr;
    Parameters.ParamByName('ndsinvoice').Value := ndsinvoice;
    Parameters.ParamByName('totalcostinv').Value := totalcostinv;
    Parameters.ParamByName('totalcostvatinv').Value := totalcostvatinv;
    Parameters.ParamByName('filial').Value := filial;
    Parameters.ParamByName('svplace').Value := svplace;
    Parameters.ParamByName('numbuhspr').Value := numbuhspr;
    Parameters.ParamByName('numdocpart').Value := numdocpart;
    ExecSQL;
  except
    on e: Exception do
      raise EInsSvodInvException.Create('Ошибка вставки записи во временную' +
        ' таблицу #svod_inv с текстом: ' + e.Message);
  end;
end;

procedure TCompDM.InsertTmpComeralInv(idPredpr, invoicefullname, datetrans,
  status, recipientname, signerfio, namepredpr, docnumber, docdate, ndsexcel,
  ndsspr, ndsinvoice, totalcostinv, totalcostvatinv, filial, svplace, numbuhspr, numdocpart: Variant);
begin
  // Вставка записи в таблицу tmpComeralInv
  with InsTmpComeralInvQ do
  try
    Parameters.ParamByName('idPredpr').Value := idPredpr;
    Parameters.ParamByName('invoicefullname').Value := invoicefullname;
    Parameters.ParamByName('datetrans').Value := datetrans;
    Parameters.ParamByName('status').Value := status;
    Parameters.ParamByName('recipientname').Value := recipientname;
    Parameters.ParamByName('signerfio').Value := signerfio;
    Parameters.ParamByName('namepredpr').Value := namepredpr;
    Parameters.ParamByName('docnumber').Value := docnumber;
    Parameters.ParamByName('docdate').Value := docdate;
    Parameters.ParamByName('ndsexcel').Value := ndsexcel;
    Parameters.ParamByName('ndsspr').Value := ndsspr;
    Parameters.ParamByName('ndsinvoice').Value := ndsinvoice;
    Parameters.ParamByName('totalcostinv').Value := totalcostinv;
    Parameters.ParamByName('totalcostvatinv').Value := totalcostvatinv;
    Parameters.ParamByName('filial').Value := filial;
    Parameters.ParamByName('svplace').Value := svplace;
    Parameters.ParamByName('numbuhspr').Value := numbuhspr;
    Parameters.ParamByName('numdocpart').Value := numdocpart;
    ExecSQL;
  except
    on e: Exception do
      raise EInsTmpComeralInvException.Create('Ошибка вставки записи в' +
        ' таблицу tmpComeralInv с текстом: ' + e.Message);
  end;
end;

procedure TCompDM.InsertSprProv(numbuhspr, numdoc, numdocpart, NSF: string;
  datadoc: TDateTime; summ: Currency; globalcode: Variant; filial: SmallInt);
begin
  // Вставка записи во временную таблицу #svod_prov
  with InsSprProvQ do
  try
    Parameters.ParamByName('numbuhspr').Value := numbuhspr;
    Parameters.ParamByName('numdoc').Value := numdoc;
    Parameters.ParamByName('numdocpart').Value := numdocpart;
    Parameters.ParamByName('NSF').Value := NSF;
    Parameters.ParamByName('datadoc').Value := datadoc;
    Parameters.ParamByName('summ').Value := summ;
    Parameters.ParamByName('globalcode').Value := globalcode;
    Parameters.ParamByName('filial').Value := filial;
    ExecSQL;
  except
    on e: Exception do
      raise EInsSprProvException.Create('Ошибка вставки записи во временную' +
        ' таблицу #svod_prov с текстом: ' + e.Message);
  end;
end;

procedure TCompDM.InsertTpmComeralProv(numbuhspr, numdoc, numdocpart, NSF,
  datadoc, summ, globalcode, filial: Variant);
begin
  // Вставка записи в таблицу tmpComeralProv
  with InsTmpComeralProvQ do
  try
    Parameters.ParamByName('numbuhspr').Value := numbuhspr;
    Parameters.ParamByName('numdoc').Value := numdoc;
    Parameters.ParamByName('numdocpart').Value := numdocpart;
    Parameters.ParamByName('NSF').Value := NSF;
    Parameters.ParamByName('datadoc').Value := datadoc;
    Parameters.ParamByName('summ').Value := summ;
    Parameters.ParamByName('globalcode').Value := globalcode;
    Parameters.ParamByName('filial').Value := filial;
    ExecSQL;
  except
    on e: Exception do
      raise EInsTmpComeralProvException.Create('Ошибка вставки записи в' +
        ' таблицу tmpComeralProv с текстом: ' + e.Message);
  end;
end;

function TCompDM.UpdateDecimalSeparator(const s: string): string;
var
  no_spaces: string;
begin
  // Изменение разделителя целой и дробной части в зависимости от системного
  // разделителя
  if Length(s) = 0 then
    Result := '0'
  else
    with FormatSettings do
    begin
      no_spaces := StringReplace(s, ' ', '', [rfReplaceAll]);

      case DecimalSeparator of
        ',':
          Result := StringReplace(no_spaces, '.', DecimalSeparator, [rfReplaceAll, rfIgnoreCase]);
        '.':
          Result := StringReplace(no_spaces, ',', DecimalSeparator, [rfReplaceAll, rfIgnoreCase]);
      end;
    end;
end;

function TCompDM.GetStatusText(Index: SmallInt): string;
begin
  case Index of
    0:
      Result := 'Нет данных';
    1:
      Result := 'В разработке';
    2:
      Result := 'В разработке. Ошибка';
    3:
      Result := 'Выставлен';
    4:
      Result := 'На согласовании';
    5:
      Result := 'Выставлен.Подписан получателем';
    6:
      Result := 'Аннулирован';
    7:
      Result := 'Выставлен.Аннулирован поставщиком';
    8:
      Result := 'Не найдено';
    9:
      Result := 'Ошибка';
  else
    Result := 'Неизвестный статус';
  end;
end;

procedure TCompDM.CompareMemTableEhCalcFields(DataSet: TDataSet);
begin
  CompareMemTableEhStatusText.AsString := GetStatusText(CompareMemTableEhStatus.AsInteger);
  CompareMemTableEhndsdiff.Value := CompareMemTableEhndsexcel.AsCurrency - CompareMemTableEhndsspr.AsCurrency;
end;

procedure TCompDM.DataModuleCreate(Sender: TObject);
var
  iniFile: TIniFile;
  SvodIp, item: string;
  ObjConnections: TStringList;
  DelimPos: Byte;
begin
  if not FileExists(ExtractFilePath(ParamStr(0)) + 'Config.ini') then
    Exit;

  iniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Config.ini');
  ObjectsDict := TDictionary<string, string>.Create;
  ObjConnections := TStringList.Create;

  // Получения списка объектов из ini файла настроек
  try
    SvodIp := iniFile.ReadString('svodconnection', 'ip', '');
    iniFile.ReadSectionValues('objectsconnection', ObjConnections);

    for item in ObjConnections do
    begin
      DelimPos := Pos('=', item);
      ObjectsDict.Add(Copy(item, 1, DelimPos - 1), Copy(item, DelimPos + 1, Length(item) - DelimPos));
    end;
  finally
    iniFile.Free;
    ObjConnections.Free;
  end;

  with SvodMDBConnection do
  begin
    Close;
    ConnectionString := connstr + 'Initial Catalog=maindatabase;Data Source=' + SvodIp;
    if DEBUG then
      ConnectionString := 'Provider=SQLOLEDB.1;Password=transpass;' +
        'Persist Security Info=True;User ID=transport;' + 'Initial Catalog=maindatabase_s;Data Source=.';
    Open;
  end;
end;

end.

